const express = require('express');
const bodyParser = require('body-parser');
const reportRoutes = require('./routes/reportRoutes');
const cors = require('cors');
const path = require('path'); // 引入 path 模块
require('dotenv').config();

const app = express();
const PORT = process.env.PORT || 5000;

app.use((req, res, next) => {
    console.log(`收到请求: ${req.method} ${req.url}`);
    next();
});

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// 配置静态文件服务
app.use('/uploads', express.static(path.join(__dirname, '../uploads')));

console.log('静态文件服务路径:', path.join(__dirname, '../uploads'));

// 注册 /api/reports 路由
app.use('/api/reports', reportRoutes);

// 未匹配路由的处理逻辑，放在所有路由之后
app.use((req, res, next) => {
    console.log(`未匹配的请求: ${req.method} ${req.url}`);
    res.status(404).send({ message: '未找到该路由' });
});

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});